var constargs = {
	server: "https://sites.google.com/site/mwivnsources/images/",
	lat: 9.825285989816358,
	lng: 105.64227233886722
}

// Map Module
var mwiapp = (function () {
	var map;
	var kmlLayer;
	
	
	// And auto height
	$(document).ready(refindeContent);
	$(window).resize(refindeContent);
	$(document).ready(init);
	
	function refindeContent() {
		var newHeight = $(window).height() - 3;
		$("#map-canvas").height(newHeight);
	}
	
	function TitleControl(titleControlDiv, map) {
		titleControlDiv.id = 'title';
		var datebox = document.createElement('p');
		datebox.id = 'datebox';
		
		var modulebox = document.createElement('p');
		modulebox.id = 'modulebox';
		
		titleControlDiv.appendChild(datebox);
		titleControlDiv.appendChild(modulebox);
	}
	
	function init() {
		var iLatLng = new google.maps.LatLng(constargs.lat, constargs.lng);
		var mapOptions = {
			center: iLatLng,
			zoom: 8,
			mapTypeId: google.maps.MapTypeId.TERRAIN,
			panControl: false,
			zoomControl: false,
			overviewMapControl: true,
			mapTypeControl: true,
			mapTypeControlOptions: {
				style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
			}
		};
		
		map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
		kmlLayer = new google.maps.KmlLayer();
		
		// Addin Title Control to the map
		var titleControlDiv = document.createElement('div');
		var titleControl = new TitleControl(titleControlDiv, map);
		map.controls[google.maps.ControlPosition.TOP_LEFT].push(titleControlDiv);
	}
	
	return {
		updateKmlLayerUrl: function(url) {
			//alert('New Url: ' + url);
			kmlLayer.setUrl(url);
			kmlLayer.setMap(map);
		},
		onOffKmlLayer: function () {
			if (kmlLayer != undefined) {
				if (kmlLayer.getMap())
					kmlLayer.setMap(null);
				else
					kmlLayer.setMap(map);
			}
		},
		updateTitleControl: function (datebox, mudulebox) {
			$("#datebox").html(datebox);
			$("#modulebox").html(mudulebox);
		}
	};
})();

// Option Module
var options = (function(){
	// Objects Definition
	function Option() {
		alert('init OPtion');
		this.url = "";
		$(this).on('changelayer', function(evt, data) {
			// Notify map change kmlLayer's url
			alert("URL changed to: " + data);
			updateKmlLayerUrl(data);
		});
	}
	
	Option.prototype.getUrl = function() {
		return this.url; 
	};
	Option.prototype.setUrl = function(val) {
		this.url = val; 
	};
	
	function FloodOption(server, year, doy) {
		this.server = server;
		this.year = year;
		this.doy = doy;
		this.extension = ".kmz";
		this.modulename = "Flood";
		$(this).on('changelayer', function(evt, data) {
			// Notify map change kmlLayer's url
			mwiapp.updateKmlLayerUrl(data);
		});
	}
	
	//FloodOption.prototype = Option.prototype;
	FloodOption.prototype.setFilename = function(year, doy) {
		this.year = year;
		this.doy = doy;
		this.url = this.server + this.modulename + year + this.extension;
		$(this).triggerHandler('changelayer', [this.url]);
	};
	
	function LanduseOption(server, year) {
		this.server = server;
		this.year = year;
		this.modulename = "Landuse";
		this.extension = ".kmz";
		$(this).on('changelayer', function(evt, data) {
			// Notify map change kmlLayer's url
			mwiapp.updateKmlLayerUrl(data);
		});
	}
	
	LanduseOption.prototype.setFilename = function(year) {
		this.year = year;
		this.url = this.server + this.modulename + year + this.extension;
		$(this).triggerHandler('changelayer', [this.url]);
	};
	
	function StationsOption(server) {
		this.server = server;
		this.name = "";
		this.modulename = "Stations";
		this.extension = ".kml";
		$(this).on('changelayer', function(evt, data) {
			// Notify map change kmlLayer's url
			mwiapp.updateKmlLayerUrl(data);
		});
	}
	
	StationsOption.prototype.setFilename = function() {
		this.url = this.server + this.modulename + this.extension;
		$(this).triggerHandler('changelayer', [this.url]);
	};
	
	function DigitalElevationOption(server, year) {
		this.server = server;
		this.year = year;
		this.modulename = "Dem";
		this.extension = ".kmz";
		$(this).on('changelayer', function(evt, data) {
			// Notify map change kmlLayer's url
			mwiapp.updateKmlLayerUrl(data);
		});
	}
	
	DigitalElevationOption.prototype.setFilename = function(year) {
		this.year = year;
		this.url = this.server + this.modulename + year + this.extension;
		$(this).triggerHandler('changelayer', [this.url]);
	};
	
	var options = {
		flood: new FloodOption(constargs.server),
		landuse: new LanduseOption(constargs.server),
		stations: new StationsOption(constargs.server),
		dem: new DigitalElevationOption(constargs.server)
	};
	
	// Refine content when needed
	
	$(document).ready(refindeContent);
	$(window).resize(refindeContent);
	$(document).ready(init);
	
	function refindeContent() {
	}
	
 	function registerHandlers() {
		
		$("#sidebar").accordion({
		  heightStyle: "content"
		});
		
		$("#FloodYear, #FloodDOY").click(function() {
			options.flood.setFilename($("#FloodYear").val(), $("#FloodDOY").val());
		});
		
		$("#LanduseYear").click(function() {
			options.landuse.setFilename($("#LanduseYear").val());
		});
		
		$("#DEMYear").click(function() {
			options.dem.setFilename($("#DEMYear").val());
		});
		
		$("#StationsShowAll").click(function() {
			options.stations.setFilename();
		});
		
		$(".onofflayer").click(function() {
			mwiapp.onOffKmlLayer();
		});
	}
	
	function init() {
		// Init Options
		// Flood
		$("#FloodYear").val("2014");
		$("#FloodDOY").val("017");
		// Land Use
		$("#LanduseYear").val("2011");

		// Register Handlers
		registerHandlers();
	}
}());